package com.example.a33mvvm.video;

import androidx.lifecycle.MutableLiveData;

import com.example.base_mvvm.base.BaseRepo;
import com.example.base_mvvm.entity.CommentEntity;
import com.example.base_mvvm.entity.FollowEntity;
import com.example.base_mvvm.entity.LikeEntity;
import com.example.base_mvvm.entity.VideoEntity;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class VideoRepo extends BaseRepo {
    public void video(int page, int size, MutableLiveData<VideoEntity> success,MutableLiveData<String> failure){
        apiService.video(page,size)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<VideoEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(VideoEntity videoEntity) {
                        success.setValue(videoEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failure.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public void like(int id, MutableLiveData<LikeEntity> success,MutableLiveData<String> failure){
        apiService.like(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LikeEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(LikeEntity likeEntity) {
                        success.setValue(likeEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failure.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public void dislike(int id, MutableLiveData<LikeEntity> success,MutableLiveData<String> failure){
        apiService.disLike(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LikeEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(LikeEntity likeEntity) {
                        success.setValue(likeEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failure.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public void follow(RequestBody body, MutableLiveData<FollowEntity> success,MutableLiveData<String> failure){
        apiService.follow(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FollowEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(FollowEntity followEntity) {
                        success.setValue(followEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failure.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public void putComment(RequestBody body, MutableLiveData<CommentEntity> success,MutableLiveData<String> failure){
        apiService.put_comment(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommentEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CommentEntity commentEntity) {
                            success.setValue(commentEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                            failure.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public void getComment(int id,MutableLiveData<CommentEntity> success,MutableLiveData<String> failure){
        apiService.get_comment(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommentEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CommentEntity commentEntity) {
                        success.setValue(commentEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failure.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
